﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public class StateManage : System.Web.UI.Page
{
    private Page myPage;
    private SessionHelper sessionHelper;        //SessionHelper实例
    private ApplicationHelper appHelper;        //ApplicationHelper实例
    private QueryStringHelper queryStringHelper;//QueryStringHelper实例
    private ViewStateHelper viewStateHelper;    //ViewStateHelper实例
    private CookieHelper cookieHelper;            //CookieHelper实例
    /**/
    /// <summary>
    /// 属性，只写，获取页面引用
    /// </summary>
    public Page MyPage
    {
        set
        {
            myPage = value;
            sessionHelper.SessionPage = value;
            appHelper.ApplicationPage = value;
            queryStringHelper.QueryStringPage = value;
            cookieHelper.CookiePage = value;

        }
    }
    /**/
    /// <summary>
    /// 构造函数，初始化子类
    /// </summary>
    public StateManage()
    {

        sessionHelper = new SessionHelper();
        appHelper = new ApplicationHelper();
        queryStringHelper = new QueryStringHelper();
        viewStateHelper = new ViewStateHelper();
        cookieHelper = new CookieHelper();


    }
    /**/
    /// <summary>
    /// 属性，只读，返回QueryStringHelperClass 实例
    /// </summary>
    public QueryStringHelper QuerystringHelper
    {
        get
        {
            if (queryStringHelper == null)
            {
                throw new Exception("QuerystringHelper is null");
            }
            return queryStringHelper;
        }
    }
    /**/
    /// <summary>
    /// 属性，只读，返回SessionHelperClass实例
    /// </summary>
    public SessionHelper SeSsionHelper
    {
        get
        {
            if (sessionHelper == null)
            {
                throw new Exception("SessionHelper is null");
            }
            return sessionHelper;
        }
    }
    /**/
    /// <summary>
    /// 属性，只读，返回Application类实例
    /// </summary>
    public ApplicationHelper AppHelper
    {
        get
        {
            if (appHelper == null)
            {
                throw new Exception("ApplicationHelper is null");
            }
            return appHelper;
        }
    }
    /**/
    /// <summary>
    /// 属性，只读，返回ViewState类实例
    /// </summary>
    public ViewStateHelper ViewstateHelper
    {
        get
        {
            if (viewStateHelper == null)
            {
                throw new Exception("ViewStateHelper is null");
            }
            return viewStateHelper;
        }
    }
    /**/
    /// <summary>
    /// 属性，只读，返回CookieHelper实例
    /// </summary>
    public CookieHelper Cookiehelper
    {
        get
        {
            if (cookieHelper == null)
            {
                throw new Exception("CookieHelper is null");
            }
            return cookieHelper;
        }
    }
    /**/
    /// <summary>
    /// SessionHelper类
    /// </summary>
    public class SessionHelper
    {
        private Page sessionPage;                        //Page引用
        /**/
        /// <summary>
        /// 属性，获取Page引用
        /// </summary>
        public Page SessionPage
        {
            set { sessionPage = value; }
        }
        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        public SessionHelper()
        {

        }
        /**/
        /// <summary>
        /// 增加Session的方法
        /// </summary>
        /// <param name="key">Session Key</param>
        /// <param name="keyValue">Session Value</param>
        public virtual void Add(string key, object keyValue)
        {
            sessionPage.Session[key] = keyValue;
        }
        /**/
        /// <summary>
        /// 根据提供的Key，获取对应的Session值
        /// </summary>
        /// <param name="key">提供的Key</param>
        /// <returns>返回Session存储的值</returns>
        public virtual object GetValue(string key)
        {
            return sessionPage.Session[key];
        }
        /**/
        /// <summary>
        /// 从Session中删除信息
        /// </summary>
        /// <param name="key">提供的Key</param>
        public virtual void Remove(string key)
        {
            sessionPage.Session.Remove(key);
        }

    }
    /**/
    /// <summary>
    /// ViewState Helper Class
    /// 不能使用Page.ViewSate，受限制，
    /// 使用StateBag获取ViewState，只可以读取变量，不能写入ViewState
    /// </summary>
    public class ViewStateHelper
    {

        private StateBag myViewState;                    //View 引用
        /**/
        /// <summary>
        /// 属性，只写，设置Page引用
        /// </summary>
        public StateBag MyViewState
        {
            set { myViewState = value; }
        }
        /**/
        /// <summary>
        /// 够咱函数
        /// </summary>
        public ViewStateHelper()
        {
        }
        public int ViewStateCount
        {
            get { return myViewState.Count; }
        }
        /**/
        /// <summary>
        /// 增加ViewState变量的方法
        /// </summary>
        /// <param name="key">变量名</param>
        /// <param name="keyValue">变量值</param>
        public void Add(string key, object keyValue)
        {

            myViewState.Add(key, keyValue);
        }
        /**/
        /// <summary>
        /// 获取ViewSate变量的值
        /// </summary>
        /// <param name="key">变量名</param>
        /// <returns>返回的变量值</returns>
        public object GetValue(string key)
        {
            return myViewState[key];
        }
        /**/
        /// <summary>
        /// 删除ViewState变量的方法
        /// </summary>
        /// <param name="key">变量名</param>
        public void Remove(string key)
        {
            myViewState.Remove(key);
        }
    }
    /**/
    /// <summary>
    /// Application Helper class
    /// </summary>
    public class ApplicationHelper
    {
        private Page applicationPage;                //Page引用
        /**/
        /// <summary>
        /// 属性，只写，设置Page引用
        /// </summary>
        public Page ApplicationPage
        {
            set { applicationPage = value; }
        }
        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        public ApplicationHelper()
        {
        }
        /**/
        /// <summary>
        /// 增加Application变量的方法
        /// </summary>
        /// <param name="key">变量名</param>
        /// <param name="keyValue">变量值</param>
        public void Add(string key, object keyValue)
        {
            applicationPage.Application.Add(key, keyValue);
        }
        /**/
        /// <summary>
        /// 获取Application变量的方法
        /// </summary>
        /// <param name="key">变量名</param>
        /// <returns>返回变量值</returns>
        public object GetValue(string key)
        {
            return applicationPage.Application[key];
        }
        /**/
        /// <summary>
        /// 删除Application变量
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            applicationPage.Application.Remove(key);
        }
    }
    /**/
    /// <summary>
    /// QueryString Helper class
    /// </summary>
    public class QueryStringHelper
    {
        private Page queryStringPage;                //Page引用
        /**/
        /// <summary>
        /// 属性，只写，设置Page引用
        /// </summary>
        public Page QueryStringPage
        {
            set { queryStringPage = value; }
        }
        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        public QueryStringHelper()
        {
        }
        /**/
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="key">参数名称</param>
        /// <returns>返回参数值</returns>
        public string GetValue(string key)
        {
            return queryStringPage.Request.QueryString[key];

        }
        /**/
        /// <summary>
        /// 增加参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="keyValue"></param>
        public void Add(string key, string keyValue)
        {
            queryStringPage.Request.QueryString.Add(key, keyValue);
        }
        /**/
        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            queryStringPage.Request.QueryString.Remove(key);
        }


    }
    /**/
    /// <summary>
    /// Cookie Helper Class
    /// </summary>
     public class CookieHelper
    {
        private Page cookiePage;                //Page引用
        /**/
        /// <summary>
        /// 属性，只写，设置Page引用
        /// </summary>
        public Page CookiePage
        {
            set { cookiePage = value; }
        }
        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        public CookieHelper()
        {
        }
        /**/
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="key">参数名称</param>
        /// <returns>返回参数值</returns>
        public string GetValue(string key)
        {
            return cookiePage.Request.Cookies[key].Value;

        }
        /**/
        /// <summary>
        /// 增加参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="keyValue"></param>
        public void Add(string key, string keyValue)
        {
            HttpCookie mycookie = new HttpCookie(key, keyValue);
            cookiePage.Request.Cookies.Add(mycookie);
        }
        /**/
        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            cookiePage.Request.Cookies.Remove(key);
        }
    }
}
